package com.drug.mingkang.controller;

import com.drug.mingkang.model.Expressage;
import com.drug.mingkang.model.ImmTrackingnumber;
import com.drug.mingkang.model.User;
import com.drug.mingkang.service.ExperssageService;
import com.drug.mingkang.service.ImmTrackingnumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 物流管理
 * @author Mr.wang
 * @since 2020-08-11 12:00:12
 */
@RestController
@RequestMapping("expressage")
public class ExpressageController {

    @Autowired
    private ExperssageService experssageService;

    @Resource
    private ImmTrackingnumberService immTrackingnumberService;

    /**
     * 根据快递单号追踪快递信息
     * @param //cohr
     * @param ExpressNumber
     * @RequestParam("cohr") String cohr,
     * @return
     */
    @RequestMapping(value="findExpressageInfo.do" ,method= RequestMethod.POST,
            produces="application/json;charset=utf-8")
    @ResponseBody
    public String[] findExpressageInfo(@RequestParam("ExpressNumber") String ExpressNumber){
        ImmTrackingnumber immTrackingnumber = immTrackingnumberService.queryById(ExpressNumber);
        Map<String, String> zto = experssageService.findExpressageInfoTraces("YTO",immTrackingnumber.getTrackingnumber());
        //判定是否签收
        String signfor = "";
        for(Map.Entry<String, String> map1 :zto.entrySet()){
            String value = map1.getValue();
            int one = value.indexOf("已签收");
            if(one!=-1){
                signfor="已签收";
            }
        }
        String[] array = new String[zto.size()];
        String[] array2 = new String[3];
        int z=0;
        for(Map.Entry<String, String> map: zto.entrySet() ){
            array[z] = map.getValue();
            z++;
        }
        array2[0] = array[0];
        array2[1] =array[zto.size()-2];
        User user = new User();
        array2[2] = signfor;
        for(int y=0;y<array2.length;y++){
            System.out.println(array2[y]);
        }
        return array2;
    }

    /**
     * 根据快递单号追踪快递信息
     * @param //cohr
     * @param ExpressNumber
     * @RequestParam("cohr") String cohr,
     * @return
     */
    @RequestMapping(value="findExpressageInfoReturn" ,method= RequestMethod.POST,
            produces="application/json;charset=utf-8")
    @ResponseBody
    public String[] findExpressageInfoReturn(@RequestParam("ExpressNumber") String ExpressNumber){
        ImmTrackingnumber immTrackingnumber = immTrackingnumberService.queryById(ExpressNumber+",tui");
        Map<String, String> zto = experssageService.findExpressageInfoTraces("YTO",immTrackingnumber.getTrackingnumber());
        //判定是否签收
        String signfor = "";
        for(Map.Entry<String, String> map1 :zto.entrySet()){
            String value = map1.getValue();
            int one = value.indexOf("已签收");
            if(one!=-1){
                signfor="已签收";
            }
        }
        String[] array = new String[zto.size()];
        String[] array2 = new String[3];
        int z=0;
        for(Map.Entry<String, String> map: zto.entrySet() ){
            array[z] = map.getValue();
            z++;
        }
        array2[0] = array[0];
        array2[1] =array[zto.size()-2];
        User user = new User();
        array2[2] = signfor;
        for(int y=0;y<array2.length;y++){
            System.out.println(array2[y]);
        }
        return array2;
    }




    /**
     * 根据快递单号返回所有的信息
     * @param //cohr
     * @param ExpressNumber
     * @RequestParam("cohr") String cohr,
     * @return
     */
    @RequestMapping(value="findExpressage" ,method= RequestMethod.POST,
            produces="application/json;charset=utf-8")
    @ResponseBody
    public List<Expressage> findExpressage(@RequestParam("ExpressNumber") String ExpressNumber){
        System.out.println(ExpressNumber);
        ImmTrackingnumber immTrackingnumber = immTrackingnumberService.queryById(ExpressNumber);
        Map<String, Object> yto = experssageService.findExpressageInfo("YTO", immTrackingnumber.getTrackingnumber());
        List<Expressage> list = new ArrayList<>();
        for (Map.Entry<String, Object> map:yto.entrySet()){
            String[] split = map.toString().split("=");
            Expressage expressage = new Expressage();
            expressage.setDate(split[1]);
            expressage.setTitle(split[2]);
            list.add(expressage);
        }
        return list;
    }


    /**
     * 根据快递单号返回所有的信息
     * @param //cohr
     * @param ExpressNumber
     * @RequestParam("cohr") String cohr,
     * @return
     */
    @RequestMapping(value="findExpressageReturn" ,method= RequestMethod.POST,
            produces="application/json;charset=utf-8")
    @ResponseBody
    public List<Expressage> findExpressageReturn(@RequestParam("ExpressNumber") String ExpressNumber){
        System.out.println(ExpressNumber);
        ImmTrackingnumber immTrackingnumber = immTrackingnumberService.queryById(ExpressNumber+",tui");
        Map<String, Object> yto = experssageService.findExpressageInfo("YTO", immTrackingnumber.getTrackingnumber());
        List<Expressage> list = new ArrayList<>();
        for (Map.Entry<String, Object> map:yto.entrySet()){
            String[] split = map.toString().split("=");
            Expressage expressage = new Expressage();
            expressage.setDate(split[1]);
            expressage.setTitle(split[2]);
            list.add(expressage);
        }
        return list;
    }


    /**
     * 判定输入单号是否正确
     * @param //cohr
     * @param ExpressNumber
     * @RequestParam("cohr") String cohr,
     * @return
     */
    @RequestMapping(value="findExpressage1" ,method= RequestMethod.POST,
            produces="application/json;charset=utf-8")
    @ResponseBody
    public int findExpressage1(@RequestParam("ExpressNumber") String ExpressNumber){
        System.out.println(ExpressNumber);
        Map<String, Object> yto = experssageService.findExpressageInfo("YTO",ExpressNumber);
        return  yto.size();
    }

    /**
     * 判定输入单号是否正确
     * @param //cohr
     * @param q1
     * @RequestParam("cohr") String cohr,
     * @return
     */
    @GetMapping("findExpressage2")
    public int findExpressage2(@RequestParam("q1") String q1, @RequestParam("q2") String q2, HttpServletRequest request,HttpServletResponse response){
        System.out.println(q1);
        System.out.println(q2);
        String wuliu = q1;
        String id = q2+",tui";
        ImmTrackingnumber immTrackingnumber = new ImmTrackingnumber();
        immTrackingnumber.setPurchaseId(id);
        immTrackingnumber.setTrackingnumber(q1);
        immTrackingnumberService.insert(immTrackingnumber);
        try {
            response.sendRedirect("../ok-admin/pages/salesReturn/immPurchase.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  1;
    }


}
